package com.fast.common.custom;


import com.fast.common.result.ApiCode;
import com.fast.common.result.ApiHelper;
import com.fast.common.result.ApiResult;
import io.undertow.util.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 * @Auther: lhy
 * @Date: 2021/11/11 15:12
 * @Mail: 190643201@qq.com
 */
@Slf4j
@RestControllerAdvice

public class GlobalExceptionCustom {


    /**
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        // 打印堆栈信息
        log.error("MethodArgumentNotValidException == " + e.getMessage());
        BindingResult result = e.getBindingResult();
        if (result.hasErrors()) {
            String msg = result.getFieldErrors().get(0).getDefaultMessage();
            return ApiHelper.FAIL(ApiCode.FAIL_NOT_VAlID.getCode(), msg);
        }
        return ApiHelper.FAIL(ApiCode.FAIL_NOT_VAlID.getCode(), e.getMessage());
    }


    /**
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ApiResult httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        // 打印堆栈信息
        log.error("HttpRequestMethodNotSupportedException == " + e.getMessage());
        return ApiHelper.FAIL(ApiCode.FAIL_NOT_METHOD.getCode(), e.getMessage());
    }


    /**
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ApiResult handleException(Exception e) {
        // 打印堆栈信息
        e.printStackTrace();
        log.error("Exception == " + e.getMessage());
        return ApiHelper.FAIL(ApiCode.FAIL.getCode(), e.getMessage());
    }


    /**
     * 处理 接口无权访问异常AccessDeniedException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public ApiResult handleAccessDeniedException(AccessDeniedException e) {
        // 打印堆栈信息
        log.error("AccessDeniedException == " + e.getMessage());
        return ApiHelper.FAIL(ApiCode.NO_PERMISSION.getCode(), ApiCode.NO_PERMISSION.getMsg());
    }

    /**
     * 登录鉴权异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BaseAuthException.class)
    public ApiResult authException(BaseAuthException e) {
        // 打印堆栈信息
        log.error("AuthException == " + e.getMessage());
        return ApiHelper.FAIL(e.getCode(), e.getMessage());
    }


    /**
     * 处理自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BadRequestException.class)
    public ApiResult badRequestException(BadRequestException e) {
        // 打印堆栈信息
        log.error("BadRequestException == " + e.getMessage());
        return ApiHelper.FAIL(ApiCode.FAIL.getCode(), e.getMessage());
    }


}
